package com.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manage.common.result.Result;
import com.manage.dao.*;
import com.manage.pojo.dto.ElectronicFileDTO;
import com.manage.pojo.entity.*;
import com.manage.pojo.entity.electronicFile.CaseInfo;
import com.manage.pojo.vo.GetAllCaseStageInfoByCaseIdVO;
import com.manage.service.ElectronicFileService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.TreeSet;

@Service
public class ElectronicFileServiceImpl implements ElectronicFileService {

    @Autowired
    private ElectronicFileMapper electronicFileMapper;

    @Autowired
    private CmBeforeLitigationMapper cmBeforeLitigationMapper;

    @Autowired
    private CmLitigationMapper cmLitigationMapper;

    @Autowired
    private CmExecuteMapper cmExecuteMapper;

     @Autowired
    private CmConvictedNotExecutedMapper cmConvictedNotExecutedMapper;



    @Override
    public Result getCaseInfoById(long id) {
        if(electronicFileMapper.checkCaseId(id) == 0 ){
            return Result.fail("案件不存在");
        }

        //查案件信息
        CaseInfo caseInfo = electronicFileMapper.selectCaseInfoById(id);
        if(caseInfo == null){
            return Result.fail("案件信息不存在");
        }else {
            return Result.success(caseInfo);
        }
    }

    @Override
    public Result getCaseStageDetailByCaseStage(ElectronicFileDTO electronicFileDTO) {
        switch (electronicFileDTO.getTable()){
            case "诉前":
                QueryWrapper<CmBeforeLitigation> cmBeforeLitigationQueryWrapper = new QueryWrapper<>();
                cmBeforeLitigationQueryWrapper.eq("case_id", electronicFileDTO.getCaseId());
                CmBeforeLitigation cmBeforeLitigation = cmBeforeLitigationMapper.selectOne(cmBeforeLitigationQueryWrapper);
                if (cmBeforeLitigation == null) {
                    return Result.fail("诉前信息不存在");
                } else {
                    return Result.success(cmBeforeLitigation);
                }
            case "诉讼":
                QueryWrapper<CmLitigation> cmLitigationQueryWrapper = new QueryWrapper<>();
                cmLitigationQueryWrapper.eq("case_id", electronicFileDTO.getCaseId());
                CmLitigation cmLitigation = cmLitigationMapper.selectOne(cmLitigationQueryWrapper);
                if (cmLitigation == null) {
                    return Result.fail("诉讼信息不存在");
                } else {
                    return Result.success(cmLitigation);
                }
            case "已判未执行":
                QueryWrapper<CmConvictedNotExecuted> cmConvictedNotExecutedQueryWrapper = new QueryWrapper<>();
                cmConvictedNotExecutedQueryWrapper.eq("case_id", electronicFileDTO.getCaseId());
                CmConvictedNotExecuted cmConvictedNotExecuted = cmConvictedNotExecutedMapper.selectOne(cmConvictedNotExecutedQueryWrapper);
                if (cmConvictedNotExecuted == null) {
                    return Result.fail("已判未执行信息不存在");
                } else {
                    return Result.success(cmConvictedNotExecuted);
                }
            case "执行":
                QueryWrapper<CmExecute> cmExecuteQueryWrapper = new QueryWrapper<>();
                cmExecuteQueryWrapper.eq("case_id", electronicFileDTO.getCaseId());
                CmExecute cmExecute = cmExecuteMapper.selectOne(cmExecuteQueryWrapper);
                if (cmExecute == null) {
                    return Result.fail("执行信息不存在");
                } else {
                    return Result.success(cmExecute);
                }
            default:
                return Result.fail("案件阶段标志不存在");
        }
    }

    @Override
    public Result getAllCaseStageInfoByCaseId(long caseId) {
        TreeSet<GetAllCaseStageInfoByCaseIdVO> getAllCaseStageInfoByCaseIdVOS = new TreeSet<>((a, b) -> a.getCaseStageId().compareTo(b.getCaseStageId()));
        getAllCaseStageInfoByCaseIdVOS.addAll(electronicFileMapper.selectAllCaseStageInfoByCaseId(caseId));
        if(getAllCaseStageInfoByCaseIdVOS.isEmpty()){
            return Result.fail("案件进度的所有阶段信息不存在");
        }else {
            return Result.success(getAllCaseStageInfoByCaseIdVOS);
        }
    }


}
